home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / mega src / Source / eq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-17  |  3.6 KB  |  168 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     eq.c
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. /* ======================================== */
  16.  
  17. // possible choices: DO_EQ, DO_NE, DO_GT, DO_LT, DO_GE, DO_LE
  18.  
  19. #define DO_LE
  20.  
  21. /* ======================================== */
  22.  
  23. #include "nshc.h"
  24.  
  25. #include "arg_utl.proto.h"
  26. #include "nshc_utl.proto.h"
  27. #include "str_utl.proto.h"
  28.  
  29. /* ======================================== */
  30.  
  31. void eq_name( t_nshc_calls *nshc_calls );
  32.  
  33. void eq_name( t_nshc_calls *nshc_calls )
  34. {
  35. #if defined DO_EQ
  36.     nshc_calls->NSH_putStr_err( "\p.eq." );
  37. #elif defined DO_NE
  38.     nshc_calls->NSH_putStr_err( "\p.ne." );
  39. #elif defined DO_GT
  40.     nshc_calls->NSH_putStr_err( "\p.gt." );
  41. #elif defined DO_LT
  42.     nshc_calls->NSH_putStr_err( "\p.lt." );
  43. #elif defined DO_LE
  44.     nshc_calls->NSH_putStr_err( "\p.le." );
  45. #elif defined DO_GE
  46.     nshc_calls->NSH_putStr_err( "\p.ge." );
  47. #endif
  48. }
  49.             
  50. /* ========================================== */
  51.  
  52. int    eq_str_less( char *p, char *q );
  53.  
  54. int    eq_str_less( char *p, char *q )
  55. {
  56.     char jim,bob;
  57.     
  58.     do {
  59.         jim = *p++;
  60.         bob = *q++;
  61.         if ( jim != bob )
  62.             return( jim < bob );
  63.     } while ( jim );
  64.             
  65.     return(0);
  66. }
  67.  
  68. /* ========================================== */
  69.  
  70. int    eq_str_more( char *p, char *q );
  71.  
  72. int    eq_str_more( char *p, char *q )
  73. {
  74.     char jim,bob;
  75.     
  76.     do {
  77.         jim = *p++;
  78.         bob = *q++;
  79.         if ( jim != bob )
  80.             return( jim > bob );
  81.     } while ( jim );
  82.             
  83.     return(0);
  84. }
  85. /* ======================================== */
  86.  
  87. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  88. {
  89.     int        numeric;
  90.     char    *str1;
  91.     char    *str2;
  92.     long    value1;
  93.     long    value2;
  94.     int        result;
  95.     
  96.     result = -1;
  97.     
  98.     // exit if the version of our nshc.h does not match the one the application used.
  99.  
  100.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) return;
  101.     
  102.     if ( nshc_parms->argc != 3 ) {
  103.         nshc_calls->NSH_putStr_err( "\pUsage: " );
  104.         eq_name( nshc_calls );
  105.         nshc_calls->NSH_putStr_err( "\p Value1 Value2\r" );
  106.         nshc_parms->result = NSHC_ERR_PARMS;
  107.         nshc_parms->action = nsh_idle;
  108.         return;
  109.         }
  110.     
  111.     numeric = nshc_is_numeric_operand(nshc_parms,1) &&
  112.               nshc_is_numeric_operand(nshc_parms,2);
  113.  
  114.     if (numeric) {
  115.         value1 =  arg_to_int(nshc_parms,1);
  116.         value2 =  arg_to_int(nshc_parms,2);
  117.         #if defined DO_EQ
  118.             result = !( value1 == value2 );
  119.         #elif defined DO_NE
  120.             result = !( value1 != value2 );
  121.         #elif defined DO_GT
  122.             result = !( value1 > value2 );
  123.         #elif defined DO_LT
  124.             result = !( value1 < value2 );
  125.         #elif defined DO_LE
  126.             result = !( value1 <= value2 );
  127.         #elif defined DO_GE
  128.             result = !( value1 >= value2 );
  129.         #endif
  130.         }
  131.     else {
  132.         str1 = &nshc_parms->arg_buf[ nshc_parms->argv[1] ];
  133.         str2 = &nshc_parms->arg_buf[ nshc_parms->argv[2] ];
  134.         #if defined DO_EQ
  135.             result = !( cStrEqual( str1, str2 ) );
  136.         #elif defined DO_NE
  137.             result =  ( cStrEqual( str1, str2 ) );
  138.         #elif defined DO_GT
  139.             result = !( eq_str_more( str1, str2 ) );
  140.         #elif defined DO_LT
  141.             result = !( eq_str_less( str1, str2 ) );
  142.         #elif defined DO_LE
  143.             if ( cStrEqual( str1, str2 ) )
  144.                 result = 0;
  145.             else
  146.                 if ( eq_str_less( str1, str2 ) )
  147.                     result = 0;
  148.                 else
  149.                     result = 1;
  150.         #elif defined DO_GE
  151.             if ( cStrEqual( str1, str2 ) )
  152.                 result = 0;
  153.             else
  154.                 if ( eq_str_more( str1, str2 ) )
  155.                     result = 0;
  156.                 else
  157.                     result = 1;
  158.         #endif
  159.         }
  160.         
  161.       nshc_parms->result = result;
  162.         
  163.     // tell the application we are done - for any requested action
  164.  
  165.     nshc_parms->action = nsh_idle;
  166. }
  167.  
  168.